package userlogcontroller

import (
	"context"
	"encoding/json"
	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/security/code"
	"vpn-management-server/src/model/userlogmodel"
	"vpn-management-server/src/service/userlogservice"
	"vpn-management-server/src/service/userservice"
)

func GetPageNormalLogsByTimestamp(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if getLogs, ok := request.GetModel().(*userlogmodel.GetPageLogs); ok {
		var resData = struct {
			NormalLogs []*userlogmodel.UserLog
			Count      int
		}{}
		userLogs, count := userlogservice.GetPageNormalLogsByTimestamp(getLogs)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user logger info fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user logger info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("user normal log is empty")
		}
		resData.NormalLogs = userLogs
		resData.Count = count
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to GetPageLogs fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to GetPageLogs fail"
		return
	}
}

func GetPageWarningLogsByTimestamp(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if getLogs, ok := request.GetModel().(*userlogmodel.GetPageLogs); ok {
		var resData = struct {
			WarningLogs []*userlogmodel.UserLog
			Count       int
		}{}
		warningLogs, count := userlogservice.GetPageWarningLogsByTimeStamp(getLogs)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user logger info fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get warning logger info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("user warning log is empty")
		}
		resData.WarningLogs = warningLogs
		resData.Count = count
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to GetPageLogs fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to GetPageLogs fail"
		return
	}
}

func GetAllNormalLogsByTimestamp(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if getLogs, ok := request.GetModel().(*userlogmodel.GetAllLogs); ok {
		var resData = struct {
			NormalLogs []*userlogmodel.UserLog
			Count      int
		}{}
		userLogs, count := userlogservice.GetAllNormalLogsByTimestamp(getLogs)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user logger info fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user logger info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("user normal log is empty")
		}
		resData.NormalLogs = userLogs
		resData.Count = count
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to GetAllLogs fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to GetAllLogs fail"
		return
	}
}

func GetAllWarningLogsByTimestamp(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if getLogs, ok := request.GetModel().(*userlogmodel.GetAllLogs); ok {
		var resData = struct {
			WarningLogs []*userlogmodel.UserLog
			Count       int
		}{}
		warningLogs, count := userlogservice.GetAllWarningLogsByTimestamp(getLogs)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user logger info fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get warning logger info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("user warning log is empty")
		}
		resData.WarningLogs = warningLogs
		resData.Count = count
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to GetAllLogs fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to GetAllLogs fail"
		return
	}
}

func GetPageNormalLogsByUGroupIDAndTimestamp(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if getLogsByUgroupID, ok := request.GetModel().(*userlogmodel.GetPageLogsByUGroupID); ok {
		var resData = struct {
			NormalLogs []*userlogmodel.UserLog
			Count      int
		}{}
		userLogs, count := userlogservice.GetPageNormalLogsByUGroupIDAndTimestamp(getLogsByUgroupID)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user logger info fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user logger info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("user normal log is empty")
		}
		resData.NormalLogs = userLogs
		resData.Count = count
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to GetPageLogsByUGroupID fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to GetPageLogsByUGroupID fail"
		return
	}
}

func GetPageWarningLogsByUGroupIDAndTimestamp(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if getLogsByUgroupID, ok := request.GetModel().(*userlogmodel.GetPageLogsByUGroupID); ok {
		var resData = struct {
			WarningLogs []*userlogmodel.UserLog
			Count       int
		}{}
		userLogs, count := userlogservice.GetPageWarningLogsByUGroupIDAndTimestamp(getLogsByUgroupID)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user logger info fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user logger info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("user user warning log is empty")
		}
		resData.WarningLogs = userLogs
		resData.Count = count
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to GetPageLogsByUGroupID fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to GetPageLogsByUGroupID fail"
		return
	}
}

func GetAllNormalLogsByUGroupIDAndTimestamp(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if getLogsByUGroupID, ok := request.GetModel().(*userlogmodel.GetAllLogsByUGroupID); ok {
		var resData = struct {
			NormalLogs []*userlogmodel.UserLog
			Count      int
		}{}
		userLogs, count := userlogservice.GetAllNormalLogsByUGroupIDAndTimestamp(getLogsByUGroupID)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user logger info fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user logger info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("user normal log is empty")
		}
		resData.NormalLogs = userLogs
		resData.Count = count
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to GetAllLogsByUGroupID fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to GetAllLogsByUGroupID fail"
		return
	}
}

func GetAllWarningLogsByUGroupIDAndTimestamp(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if getLogsByUGroupID, ok := request.GetModel().(*userlogmodel.GetAllLogsByUGroupID); ok {
		var resData = struct {
			WarningLogs []*userlogmodel.UserLog
			Count       int
		}{}
		userLogs, count := userlogservice.GetAllWarningLogsByUGroupIDAndTimestamp(getLogsByUGroupID)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user logger info fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user logger info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("user user warning log is empty")
		}
		resData.WarningLogs = userLogs
		resData.Count = count
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to GetAllLogsByUGroupID fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to GetAllLogsByUGroupID fail"
		return
	}
}

func GetUserLogAnalysis(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {

}

func GetPageNormalLogsByUserIDAndTimestamp(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if getLogsByUserID, ok := request.GetModel().(*userlogmodel.GetLogsByUserID); ok {
		var resData = struct {
			NormalLogs []*userlogmodel.UserLog
			Count      int
		}{}
		user := userservice.GetUserByID(getLogsByUserID.UserID)
		if user == nil {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user by id fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user by id fail"
			return
		}
		var getLogsByUserName userlogmodel.GetLogsByUserName
		getLogsByUserName.GetPageLogs = getLogsByUserID.GetPageLogs
		getLogsByUserName.UserName = user.Name
		userLogs, count := userlogservice.GetPageNormalLogsByUserNameAndTimestamp(&getLogsByUserName)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user logger info fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user logger info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("user user normal log is empty")
		}
		resData.NormalLogs = userLogs
		resData.Count = count
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to GetLogsByUserID fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to GetLogsByUserID fail"
		return
	}
}

func GetPageWarningLogsByUserIDAndTimestamp(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if getLogsByUserID, ok := request.GetModel().(*userlogmodel.GetLogsByUserID); ok {
		var resData = struct {
			WarningLogs []*userlogmodel.UserLog
			Count       int
		}{}
		user := userservice.GetUserByID(getLogsByUserID.UserID)
		if user == nil {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user by id fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user by id fail"
			return
		}
		var getLogsByUserName userlogmodel.GetLogsByUserName
		getLogsByUserName.GetPageLogs = getLogsByUserID.GetPageLogs
		getLogsByUserName.UserName = user.Name
		warningLogs, count := userlogservice.GetPageWarningLogsByUserNameAndTimestamp(&getLogsByUserName)
		if count == -1 {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get user logger info fail")
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user logger info fail"
			return
		}
		if count == 0 {
			logger.Logger.Warn("user user warning log is empty")
		}
		resData.WarningLogs = warningLogs
		resData.Count = count
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to GetLogsByUserID fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to GetLogsByUserID fail"
		return
	}
}

// UpLoadNormalLog 接收vpnserver转发过来的数据 然后再次转发给区块链存储上链
func UpLoadNormalLog(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if userLog, ok := request.GetModel().(*userlogmodel.UserLog); ok {
		userlogservice.UpLoadUserLog(userLog)
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to UserLog fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to UserLog fail"
		return
	}
}

//func UpLoadWaringLog(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
//	if warningLog, ok := request.GetModel().(*userlogmodel.UserLog); ok {
//		userlogservice.UpLoadWaringLog(warningLog)
//	} else {
//		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to UserLog fail")
//		response.Code = code.BAD_REQUEST
//		response.ErrorMsg = "transform interface to UserLog fail"
//		return
//	}
//}
